package dmsxl.erchashu;

/**
 * Author: Zhang Dongwei
 * Date: 2023/5/20 16:17
 * 二叉搜索树的最小绝对差
 */
public class ecs24_530 {

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(4);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(6);
        TreeNode node4 = new TreeNode(1);
        TreeNode node5 = new TreeNode(3);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        System.out.println(getMinimumDifference(node1));
    }

    static int result = Integer.MAX_VALUE;
    static TreeNode pre = null;
    public static int getMinimumDifference(TreeNode root) {
        getMD(root);
        return result;
    }

    public static void getMD(TreeNode cur){
        if (cur == null) return;
        getMD(cur.left);
        if (pre != null){
            result = Math.min(result, cur.val - pre.val);
        }
        pre = cur;
        getMD(cur.right);
    }
}
